package io.renren.modules.canteens.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.Result;
import io.renren.modules.canteens.dao.BatchOrdertemDao;
import io.renren.modules.canteens.dto.BatchOrdertemDTO;
import io.renren.modules.canteens.dto.FoodDTO;
import io.renren.modules.canteens.entity.BatchOrdertemEntity;
import io.renren.modules.canteens.excel.BatchOrdertemExcel;
import io.renren.modules.canteens.service.BatchOrdertemService;
import io.renren.modules.canteens.service.FoodService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 批量采购明临时表 
 *
* @author 翟文闯 619556390@QQ.com
 * @since 1.0.0 2020-01-06
 */
@Service
public class BatchOrdertemServiceImpl extends CrudServiceImpl<BatchOrdertemDao, BatchOrdertemEntity, BatchOrdertemDTO> implements BatchOrdertemService {
    @Autowired
    private BatchOrdertemService batchOrdertemService;
    @Autowired
    private BatchOrdertemDao batchOrdertemDao;
    @Autowired
    private FoodService foodService;
    @Override
    public QueryWrapper<BatchOrdertemEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<BatchOrdertemEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Transactional
    public Result savemportAmount(List<BatchOrdertemExcel> list) {
        //清空表
        batchOrdertemDao.deleteTable();
        Result result = new Result();
        for (BatchOrdertemExcel excel:list) {
            BatchOrdertemDTO ordertemDTO = new BatchOrdertemDTO();
            FoodDTO foodDTO = foodService.getName(excel.getFoodName());
            BeanUtils.copyProperties(excel,ordertemDTO);
            if(null == foodDTO){
                result.setCode(5000);
                result.setMsg(excel.getFoodName()+"菜品不存在");
                return result ;
            }
            if(null == excel.getUnitPrice()){
                result.setCode(5000);
                result.setMsg(excel.getFoodName()+excel.getUnitPrice()+"格式异常");
                return result ;
            }
            if(null == excel.getPurchaseNum()){
                result.setCode(5000);
                result.setMsg(excel.getFoodName()+excel.getPurchaseNum()+"格式异常");
                return result ;
            }
            ordertemDTO.setMoney(excel.getUnitPrice()*excel.getPurchaseNum());
            ordertemDTO.setFoodId(foodDTO.getFoodId());
            ordertemDTO.setUnit(foodDTO.getUnit());
            batchOrdertemService.save(ordertemDTO);
        }
        result.setCode(0);
        result.setMsg("导入成功");
        return result;
    }

    @Override
    public List<BatchOrdertemDTO> getBatchOrdermx() {
        List<BatchOrdertemDTO> batchOrdermx = batchOrdertemDao.getBatchOrdermx();
        return batchOrdermx;
    }

    @Override
    public BatchOrdertemDTO getTotalMoney() {
        BatchOrdertemDTO totalMoney = batchOrdertemDao.getTotalMoney();
        return totalMoney;
    }

    @Override
    public void delete1(Long batchOrdertemId) {
        batchOrdertemDao.delete1(batchOrdertemId);
    }

}